package com.filetransfer.view.Components;
import com.filetransfer.config.UserThemeConfiguration;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;
@Slf4j
public class UploadHistorySelectionDialog extends JDialog {
    public UploadHistorySelectionDialog(JFrame parentFrame, UserThemeConfiguration config, Consumer<UserThemeConfiguration.UploadHistory> onSelect) {
        super(parentFrame, "选择上传记录", true);
        setSize(400, 300);
        setLocationRelativeTo(parentFrame);
        setLayout(new BorderLayout());
        // 列表模型和展示
        DefaultListModel<String> historyListModel = new DefaultListModel<>();
        for (UserThemeConfiguration.UploadHistory history : config.getUploadHistory()) {
            historyListModel.addElement(String.format("文件: %s | 日期: %s", history.getFileName(), history.getUploadDate()));
        }
        JList<String> historyList = new JList<>(historyListModel);
        historyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(historyList);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        add(scrollPane, BorderLayout.CENTER);
        // 确认按钮
        JButton confirmButton = new JButton("确认");
        add(confirmButton, BorderLayout.SOUTH);
        confirmButton.addActionListener(e -> {
            int selectedIndex = historyList.getSelectedIndex();
            if (selectedIndex >= 0) {
                UserThemeConfiguration.UploadHistory selectedHistory = config.getUploadHistory().get(selectedIndex);
                onSelect.accept(selectedHistory);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "请选择一条上传记录！", "提示", JOptionPane.WARNING_MESSAGE);
            }
        });
    }
}
